﻿Shader "Running/grass"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_ambientLightColor("_ambientLightColor", Color) = (1, 1, 1, 1)


		_shadowMap("shadowMap", 2D) = "white" {}

		inverseShadowmapSize("inverseShadowmapSize", Float) = 1
		fixedDepthBias("fixedDepthBias", Float) = 0
		gradientClamp("gradientClamp", Float) = 0
		gradientScaleBias("gradientScaleBias", Float) = 1

		shadowColor("shadowColor", Color) = (0, 0, 0, 1)

	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			Name "FORWARD"
			Tags{
				"LightMode" = "ForwardBase"
			}
			Lighting Off

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase

			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			#include "tools.cginc"

			//uniform float4x4 texViewProj;

			struct appdata
			{
				float4 vertex : POSITION;
				float4 uv : TEXCOORD0;
				float4 normal : NORMAL;
				float4 color : COLOR;
			};

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float4 color : COLOR;
				float4 decal1UV : TEXCOORD0;
				float4 diffuseLightColor : TEXCOORD1;
				float4 shadowUV : TEXCOORD2;
			};

			
			v2f vert (appdata v)
			{
				v2f o;
				float4 worldPos = mul(unity_ObjectToWorld, v.vertex);

				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.color = v.color;
				o.decal1UV = v.uv;
				o.diffuseLightColor = lightning(_WorldSpaceLightPos0, _LightColor0, worldPos, unity_WorldToObject, v.normal);
				o.shadowUV = mul(texViewProj, worldPos);
				return o;
			}
			
			sampler2D _MainTex;
			float4 _MainTex_ST;
			uniform float4 _ambientLightColor;

			uniform sampler2D _shadowMap;

			//uniform float inverseShadowmapSize;
			uniform float fixedDepthBias;
			uniform float gradientClamp;
			uniform float gradientScaleBias;
			uniform float4 shadowColor;

			fixed4 frag (v2f i) : SV_Target
			{
				float4 decalColor = tex2D(_MainTex, i.decal1UV) * i.color;
				clip(decalColor.a - 0.5);

				float4 result = decalColor * _ambientLightColor + decalColor *
				i.diffuseLightColor * inShadow(i.vertex, i.shadowUV, _shadowMap, inverseShadowmapSize, fixedDepthBias, gradientClamp, gradientScaleBias, shadowColor.a);
				result.a = decalColor.a;
				return result;
			}
			ENDCG
		}
	}
}
